package xdTool

import sysUtil.BeetlUtil
import sysUtil.ResultCode
import sysUtil.ResultJson
import sysUtil.SysCommUtil
import xdInterface.XdController

class XdWidgetSelectController extends XdController {



    def sysDBUtilService
    def authUtilsService

    @Override
    def detail() {
        return null
    }

    @Override
    def del() {
        return null
    }

    @Override
    def edit() {
        return null
    }

    @Override
    def save() {
        return null
    }

    @Override
    def update() {
        return null
    }

    @Override
    def add() {
        return null
    }

    @Override
    def dowLoad() {
        return null
    }

    @Override
    def upLoad() {
        return null
    }

    @Override
    def getOne() {
        return null
    }

    @Override
    def query() {
        return null
    }

    @Override
    def delete() {
        return null
    }

    /**
     * 预览
     */
    def selectView(){
        def result = ResultJson.resultJson()
        def id = Long.parseLong(params?.id)
        def allFlag = params.allFlag?:"false"
        def sql = params?.dataSql
        try {
            sql =  BeetlUtil.getSelectSQLTemplate(params,sql)
            //println sql
            def dataSource = sysDBUtilService.getGroovySqlInstance(XdDataSource.get(params?.dataSourceId))
            if(!dataSource){
                dataSource = sysDBUtilService.getGroovySqlInstance()
            }

            def data = []
            if(allFlag == "true"){ //全选
                data = sysDBUtilService.pageAllResult(sql,dataSource)
            }else{
                data = sysDBUtilService.pageResult(sql,1,10,dataSource)
            }


            def results = []
            data.each {it ->
                def map = [:]
                it.each {a,b ->
                    map.put("${String.valueOf(a).toLowerCase()}",b)
                }
                results.add(map)
            }

            result.put(ResultJson.CODE.name,ResultCode.TABLE_DATA_SUCCESS.code)
            result.put(ResultJson.MSG.name,ResultCode.TABLE_DATA_SUCCESS.message)
            result.put(ResultJson.DATA.name,results)
        }catch (Exception e){
            result.put(ResultJson.CODE.name,ResultCode.QUERY_ERROR.code)
            result.put(ResultJson.MSG.name,e.getMessage())
            e.printStackTrace()
        }
        render result
    }
}
